package com.leo.base.account;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.leo.account.R;
import com.leo.base.healthui.PersonHealthActivity;
import com.leo.base.healthui.PersonListActivity;
import com.leo.base.tools.InputCheck;
/**
 * 
 * @author Leo 20140922
 *
 */
public class AccountLoginActivity extends Activity implements OnClickListener {

	private EditText userEdit, pwdEdit;
	private Button loginBtn;
	private TextView forgotText, registerText;
	private String account, password;
   //test
	private RequestQueue mQueue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_login);
		initWidget();
	}

	private void initWidget() {
		userEdit = (EditText) findViewById(R.id.user_edit);
		pwdEdit = (EditText) findViewById(R.id.pwd_edit);
		loginBtn = (Button) findViewById(R.id.login_btn);
		forgotText = (TextView) findViewById(R.id.forgot_text);
		registerText = (TextView) findViewById(R.id.register_text);

		loginBtn.setOnClickListener(this);
		forgotText.setOnClickListener(this);
		registerText.setOnClickListener(this);

		forgotText.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		initVolley();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int viewID = view.getId();
		if (viewID == R.id.forgot_text) {
			forgotPwd();
		} else if (viewID == R.id.login_btn) {
			login();
		} else if (viewID == R.id.register_text) {
			register();
		}
		
	}

	private void gotoMainScreen()
	{
		Intent intent = new Intent(this,PersonListActivity.class);
		startActivity(intent);
	}
	private void register() {
		Intent intent = new Intent(this, AccountRegisterActivity.class);
		startActivity(intent);
	}

	private void initVolley() {
		mQueue = Volley.newRequestQueue(this);
		mQueue.start();
	}

	private void login() {

		account = userEdit.getText().toString();
		password = pwdEdit.getText().toString();
		if(!InputCheck.isMobileNO(account))
		{
			Toast.makeText(this, "phone input error", Toast.LENGTH_LONG).show();
			return;
		}
		else if(!InputCheck.isRuledPassword(password, this))
		{
			return;
		}
		new LoginTask().execute(account, password);
	}

	private void forgotPwd() {
		Intent intent = new Intent(this, AccountForgotPasswordActivity.class);
		startActivity(intent);
	}

	class LoginTask extends AsyncTask<String, Void, Boolean> {

		private BaseUser user;

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				user = AVUser.logIn(params[0], params[1], BaseUser.class);
			} catch (AVException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}

			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			if (result == true) {
				//AVUser.getCurrentUser(BaseUser.class).getObjectId();
				gotoMainScreen();
				finish();
				
			}
		}

	}
	public class LoginLisener implements Listener<JSONObject> {

		@Override
		public void onResponse(JSONObject json) {
			Log.d("leo", "LoginLisener onResponse json = " + json.toString());
			int ackCmd = 0;
		}

	}

	public class LoginErrorLisener implements ErrorListener {

		@Override
		public void onErrorResponse(VolleyError err) {
			// TODO Auto-generated method stub
			Log.d("leo", "LoginLisener onErrorResponse err = " + err);

		}

	}
}
